﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC.WPF;
using DragDropLibrary;
using ImageAppDataModels;
using ImageAppViewModels;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using BrainTechLLC.ImageOperators;
using BrainTechLLC.FolderExplorer;
using TemporalFilterExtensions;
using System.Drawing;
using System.Windows.Media;
using BrainTechLLC.FilterExtensions;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Reflection;
using System.ComponentModel;

namespace WpfImageDuplicates
{
    public partial class ImageCollectionView : UserControl
    {
        private void gridMain_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var pos = e.GetPosition((UIElement)sender);

            tracker = new MouseTracker();
            tracker.StartX = pos.X;
            tracker.StartY = pos.Y;

            IInputElement element = lstItems.InputHitTest(e.GetPosition((UIElement)sender));
            Border b = element as Border;
            if (b != null && b.Name == "sizeBorder")
            {
                tracker._Action = 1;
                var contentPresenters = (element as UIElement).GetActualChildren<ContentPresenter>();
                if (contentPresenters.Count > 0)
                {
                    Console.WriteLine(contentPresenters[0].DataContext.ToString());
                }
                gridMain.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(gridMain_PreviewMouseLeftButtonUp);
                gridMain.PreviewMouseMove += new MouseEventHandler(gridMain_PreviewMouseMove);
                mouseGrid.Visibility = System.Windows.Visibility.Visible;
                mouseGrid.DataContext = tracker;
                e.Handled = true;
            }
            else
            {
                mouseGrid.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void gridMain_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            gridMain.PreviewMouseMove -= gridMain_PreviewMouseMove;
            gridMain.PreviewMouseLeftButtonUp -= gridMain_PreviewMouseLeftButtonUp;
            var pos = e.GetPosition((UIElement)sender);
            tracker.EndX = pos.X;
            tracker.EndY = pos.Y;

            tracker.HitTestResults.Clear();
            VisualTreeHelper.HitTest((Visual)sender, null, new HitTestResultCallback(tracker.MyHitTestResult), new PointHitTestParameters(pos));

            if (tracker._Action == 1)
            {
                double deltaX = tracker.StartX - tracker.EndX;
                double deltaY = tracker.StartY - tracker.EndY;
                Settings.Values.SetThumbWidthAndHeight(Settings.Values.ThumbWidth - deltaX, Settings.Values.ThumbHeight - deltaY);
            }

            tracker.RaisePropertyChanged("DisplayCoords");
            e.Handled = true;
        }

        void gridMain_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            var pos = e.GetPosition((UIElement)sender);
            tracker.CurrentX = pos.X;
            tracker.CurrentY = pos.Y;

            tracker.HitTestResults.Clear();
            VisualTreeHelper.HitTest((Visual)sender, null, new HitTestResultCallback(tracker.MyHitTestResult), new PointHitTestParameters(pos));

            tracker.RaisePropertyChanged("DisplayCoords");
            e.Handled = true;
        }

        MouseTracker tracker = new MouseTracker();
    }
}